import request from '@/utils/request'

/**
 * 添加购物车
 */
export function addCart(data: AnyObject) {
	// return request.post(`shop/cart`, data)
	return request.post(`trade/cart/add`, data, { isYuDao: true })

}

/**
 * 编辑购物车数量
 */
export function editCart(data: AnyObject) {
	// return request.put(`shop/cart`, data)
	return request.put(`trade/cart/update-count`, data, { isYuDao: true })

}

/**
 * 删除购物车
 */
export function deleteCart(data: AnyObject) {
	console.log(data)
	// return request.put(`shop/cart/delete`, data)
	return request.delete(`trade/cart/delete?ids=${data.ids}`, {}, { isYuDao: true })

}

/**
 * 清空购物车
 */
export function clearCart() {
	return request.delete(`shop/cart/clear`)
}

/**
 * 获取购物车列表
 */
export function getCartList(params: Record<string, any>) {
	// return request.get(`shop/cart`, params)
	return request.get(`trade/cart/list`, params, { isYuDao: true })

}

/**
 * 获取购物车商品列表
 */
export function getCartGoodsList(params: Record<string, any>) {
	// return request.get(`shop/cart/goods`, params)
	return request.get(`trade/cart/list`, params, { isYuDao: true })

}

/**
 * 获取购物车数量
 */
export function getCartSum(params: Record<string, any>) {
	return request.get(`shop/cart/sum`, params)
}


/**
 * 购物车计算
 */
export function getCartCalculate(data: Record<string, any>) {
	console.log(data)
	const data2 = {
		...data,
	};
	// 移除多余字段
	if (!(data.couponId > 0)) {
		delete data2.couponId;
	}
	if (!(data.addressId > 0)) {
		delete data2.addressId;
	}
	if (!(data.pickUpStoreId > 0)) {
		delete data2.pickUpStoreId;
	}
	if (uni.$u.test.isEmpty(data.receiverName)) {
		delete data2.receiverName;
	}
	if (uni.$u.test.isEmpty(data.receiverMobile)) {
		delete data2.receiverMobile;
	}
	if (!(data.combinationActivityId > 0)) {
		delete data2.combinationActivityId;
	}
	if (!(data.combinationHeadId > 0)) {
		delete data2.combinationHeadId;
	}
	if (!(data.seckillActivityId > 0)) {
		delete data2.seckillActivityId;
	}
	if (!(data.pointActivityId > 0)) {
		delete data2.pointActivityId;
	}
	// 解决 SpringMVC 接受 List<Item> 参数的问题
	delete data2.items;
	for (let i = 0; i < data.items.length; i++) {
		data2[encodeURIComponent('items[' + i + '' + '].skuId')] = data.items[i].skuId + '';
		data2[encodeURIComponent('items[' + i + '' + '].count')] = data.items[i].count + '';
		if (data.items[i].cartId) {
			data2[encodeURIComponent('items[' + i + '' + '].cartId')] = data.items[i].cartId + '';
		}
	}
	const queryString = Object.keys(data2)
		.map((key) => key + '=' + data2[key])
		.join('&');
	// return request.post(`shop/cart/calculate`, params)
	return request.get(`trade/order/settlement?${queryString}`, {}, { isYuDao: true })



}